********************************************************************************
** Do file: g4_endline_educ_hh.do
** First started: April 22, 2020 
** Last edited: December 31, 2023

/* Purpose: this do file analyzes education outcomes at the household level

(Modified) code to create indices comes from Casey et al. (2012)

* ITEM MADE: TABLE 3
*/

********************************************************************************

clear
clear matrix
clear mata
set maxvar 10000

** Preamble - data management for both the individual level dataset and household level dataset **

* For later use: Develop a num_520 variable but for endline
use "$esvy_clean/2_educ_indiv.dta", clear
g num_520e = 1
collapse (sum) num_520e, by(hhid)
tempfile num_520e_info
save `num_520e_info'

** Household **

* Load key data first
use "$merged/key_rep.dta", clear
keep if hhid!=.

* Restrict down to sample of interest
keep if k_complete_may==1 & k_rolling_list==1 & k_interacted_success==1 & k_surveyed==1 & k_surveyed_end==1

* Endline		
merge 1:1 hhid using "$esvy_clean/2_educ_hh.dta"
keep if _merge==3 | _merge==1
drop _merge		
		
/* Change to USD (2019) https://data.worldbank.org/indicator/PA.NUS.FCRF?locations=UG */
replace schoolexpend_t2_fin = schoolexpend_t2_fin/3704
		
* Take log
g ln_schoolexpend_t2_fin=ln(schoolexpend_t2_fin+1)
		
* Drop the choice group as well
drop if treatmenttype_sh=="R T3"

* Develop indicators
g anytreat_assigned = (treatmenttype_sh=="R T1-L" | treatmenttype_sh=="R T1-U" | treatmenttype_sh=="R T2-U") 
g locked_assigned = (treatmenttype_sh=="R T1-L")
g surprise_assigned = (treatmenttype_sh=="R T1-U")
g unlocked_assigned = (treatmenttype_sh=="R T2-U")

* Merge in num_520e
merge 1:1 hhid using `num_520e_info', keepusing(num_520e)
keep if _merge==3 | _merge==1
drop _merge



************
** Part I **
************

* Input zeroes if missing for days absent, zeroes anything else
count if missed_month_t2_fin==. & anytreat_assigned!=. & enroll_t2_fin!=. // 58
count if ln_schoolexpend_t2_fin==. & anytreat_assigned!=. & enroll_t2_fin!=. // 58
replace missed_month_t2_fin = 30 if anytreat_assigned!=. & missed_month_t2_fin==. & enroll_t2_fin!=.
replace schoolexpend_t2_fin = 0 if anytreat_assigned!=. & ln_schoolexpend_t2_fin==. & enroll_t2_fin!=.
replace ln_schoolexpend_t2_fin = 0 if anytreat_assigned!=. & ln_schoolexpend_t2_fin==. & enroll_t2_fin!=.

g missed_month_t2_fin2 = -missed_month_t2_fin
		
local p_me_corevars_1 "enroll_t2_fin missed_month_t2_fin2 ln_schoolexpend_t2_fin"

* Cut to the appropriate sample
keep if enroll_t2_fin !=.
	
foreach num of numlist 1 {
local i = `num'
	* 1. Convert outcomes to effect sizes
	foreach var of varlist  `p_me_corevars_`i'' {
		local l = `l' + 1
		qui sum `var' if anytreat_assigned==0
		qui gen `var'_mean=r(mean)
		qui gen `var'_sd=r(sd)
		qui gen std_`i'_`l'=(`var'-`var'_mean)/`var'_sd
		qui egen mean_std_`i'_`l'_t=mean( std_`i'_`l') if anytreat_assigned==1 
		qui replace std_`i'_`l'=mean_std_`i'_`l'_t if anytreat_assigned==1 & std_`i'_`l'==.
		qui replace std_`i'_`l'=0 if anytreat_assigned==0 & std_`i'_`l'==.
		local matrix_string_`i'="`matrix_string_`i''" + " std_`i'_`l'"
		}
		local k = `l'
		save "$temp/mei_std_h`i'.dta", replace
		
	* 2. Construct matrix and component weights using controls only;
	drop if anytreat_assigned==1
	
		forvalues x = 1/`k' {
			gen weight`x'_`i'=0
			}
	
		matrix accum R = `matrix_string_`i'', nocons dev
		matrix R=R/r(N)
		matrix R=invsym(R)
		local counter1=1
		matrix J = J(colsof(R), 1, 1)
		while `counter1'<=colsof(R){
			matrix T = R[`counter1', 1..colsof(R)]
			matrix A = T*J
			qui replace weight`counter1'_`i'=A[1,1]
			qui replace weight`counter1'_`i'=0 if weight`counter1'_`i'<0
			local counter1 = `counter1'+1
			}
			
		gen sample_h`i'=0
		forvalues x = 1/`k' {
			qui replace sample_h`i'=sample_h`i'+weight`x'_`i'
			}
		keep sample_h`i' weight*
		collapse _all
				
		cross using "$temp/mei_std_h`i'.dta"
			
	* 3. Apply weights and construct index;
		qui gen outcome_h`i'=0
			forvalues x = 1/`k' {
			qui replace outcome_h`i'=std_`i'_`x'*(weight`x'_`i') + outcome_h`i'
			}
			qui replace outcome_h`i'=outcome_h`i'/sample_h`i'
		local l = 0
		drop *_mean *_sd mean_std_*
}
	* 4. Estimate treatment effects;
		foreach num of numlist 1 {
			reg outcome_h`num' anytreat_assigned
			estimates store ander_`num'
			}
			

*** ITT

* Generate interaction term with num_520e
g atreatnum_520e = anytreat_assigned * num_520e


***** TABLE 3 *****

* Outcome means
sum enroll_t2_fin if anytreat_assigned!=. & anytreat_assigned==0 & num_520e!=.
sum missed_month_t2_fin if anytreat_assigned!=. & anytreat_assigned==0 & num_520e!=.
sum schoolexpend_t2_fin if anytreat_assigned!=. & anytreat_assigned==0 & num_520e!=.
sum outcome_h1 if anytreat_assigned==0 & num_520e!=.

reg enroll_t2_fin locked_assigned surprise_assigned unlocked_assigned atreatnum_520e num_520e
test locked_assigned = unlocked_assigned

reg missed_month_t2_fin locked_assigned surprise_assigned unlocked_assigned atreatnum_520e num_520e
test locked_assigned = unlocked_assigned

reg ln_schoolexpend_t2_fin locked_assigned surprise_assigned unlocked_assigned atreatnum_520e num_520e
test locked_assigned = unlocked_assigned

reg outcome_h1 locked_assigned surprise_assigned unlocked_assigned atreatnum_520e num_520e
test locked_assigned = unlocked_assigned

* P values at median 3 
reg enroll_t2_fin anytreat_assigned atreatnum_520e num_520e
lincom anytreat_assigned + 3 * atreatnum_520e
	* Note that the above gives equivalent answers to lincom _b[anytreat_assigned] + 3 * _b[atreatnum_520e]
	
	reg enroll_t2_fin locked_assigned surprise_assigned unlocked_assigned atreatnum_520e num_520e
		lincom locked_assigned + 3 * atreatnum_520e
		lincom surprise_assigned + 3 * atreatnum_520e
		lincom unlocked_assigned + 3 * atreatnum_520e
		
	reg missed_month_t2_fin anytreat_assigned atreatnum_520e num_520e
	lincom anytreat_assigned + 3 * atreatnum_520e
	
	reg ln_schoolexpend_t2_fin anytreat_assigned atreatnum_520e num_520e
	nlcom exp(_b[anytreat_assigned]+3*_b[atreatnum_520e])-1
	* note that it MUST be in this format, not like before with lincom

		* Secured: 38%
		reg ln_schoolexpend_t2_fin locked_assigned surprise_assigned unlocked_assigned atreatnum_520e num_520e
		nlcom exp(_b[locked_assigned]+3*_b[atreatnum_520e])-1	
	
reg outcome_h1 anytreat_assigned atreatnum_520e num_520e
lincom anytreat_assigned + 3 * atreatnum_520e
	
* OLS results

eststo: reg enroll_t2_fin anytreat_assigned atreatnum_520e num_520e
eststo: reg enroll_t2_fin locked_assigned surprise_assigned unlocked_assigned atreatnum_520e num_520e

eststo: reg missed_month_t2_fin anytreat_assigned atreatnum_520e num_520e
eststo: reg missed_month_t2_fin locked_assigned surprise_assigned unlocked_assigned atreatnum_520e num_520e

eststo: reg ln_schoolexpend_t2_fin anytreat_assigned atreatnum_520e num_520e
eststo: reg ln_schoolexpend_t2_fin locked_assigned surprise_assigned unlocked_assigned atreatnum_520e num_520e

eststo: reg outcome_h1 anytreat_assigned atreatnum_520e num_520e
eststo: reg outcome_h1 locked_assigned surprise_assigned unlocked_assigned atreatnum_520e num_520e

esttab using "$tables/endline_educ_hh.tex", ///
	b(2) se(2) se replace booktabs star(* .10 ** .05 *** .01) title(Education Outcomes, Household-level\label{endline_educ_hh}) ///
	label nodepvars mtitles("Enrollment" "Enrollment" "Days absent" "Days absent" "Log school expenditures" "Log school expenditures" "Education index" "Education index") nonotes ///
	order(anytreat_assigned locked_assigned surprise_assigned unlocked_assigned atreatnum_520e num_520e _cons) ///
	varlabels(anytreat_assigned "Pooled" locked_assigned "Secured" surprise_assigned "Surprise Unsecured" unlocked_assigned "Unsecured" _cons "Constant" num_520e "N SAC at endline" atreatnum_520e "Pooled $\times$ Number of School-Aged Children", ///
	elist(anytreat_assigned \addlinespace locked_assigned \addlinespace surprise_assigned \addlinespace unlocked_assigned \addlinespace atreatnum_520e \addlinespace num_520e \addlinespace)) 
eststo clear
